package com.keyinong.fragment;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.keyinong.restaurant.R;
import com.keyinong.restaurant.entity.DataBean;
import com.keyinong.restaurant.entity.RechargeType;
import com.keyinong.restaurant.entity.UserInfoDataBean;
import com.keyinong.restaurant.tools.HttpTools;
import com.loopj.android.http.JsonHttpResponseHandler;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
//import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class RechargeFragment extends Fragment implements OnClickListener
,OnItemSelectedListener {
	
	private View mview;
	private TextView tv_name;
	private TextView tv_phone;
	private TextView tv_balance;
	private EditText et_money;
	private Button btn_recharge;
	private Spinner sn_rechargeType;
	
	private HttpTools httpTools=null;
	private UserInfoDataBean user;
	private String token="";
	private ArrayList<String> types=new ArrayList<String>();
	private int index=1;
	
	
	
	
	@Override
	public View onCreateView(LayoutInflater inflater,
			ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		mview=inflater.inflate(R.layout.fragment_recharge_layout, container,false);
		return mview;
	}
	
	@Override
	public void onActivityCreated( Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		initView();
	}

	private void initView() {
		tv_name=(TextView) mview.findViewById(R.id.tv_name);
		tv_phone=(TextView) mview.findViewById(R.id.tv_phone);
		tv_balance=(TextView) mview.findViewById(R.id.tv_balance);
		et_money=(EditText) mview.findViewById(R.id.et_money);
		btn_recharge=(Button) mview.findViewById(R.id.btn_recharge);
		sn_rechargeType=(Spinner) mview.findViewById(R.id.sn_rechargeType);
		
		btn_recharge.setOnClickListener(this);
		addTypes();
		
		sn_rechargeType.setOnItemSelectedListener(this);
		
		
	}
	private void addTypes(){
		types.add("现金");
		types.add("餐卡");
		types.add("支付宝");
		
		ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_spinner_item,types);
		adapter.setDropDownViewResource
		(android.R.layout.simple_spinner_dropdown_item);
		sn_rechargeType.setAdapter(adapter);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_recharge:
			recharge();
			break;

		}
		
	}
	
	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		   index=position+1;
		   Log.e("lh", "index=="+index);
		
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void onStart() {
		super.onStart();
		try{
			String scan_res=getArguments().getString("scan_res");
			
			if(!TextUtils.isEmpty(scan_res)){
				
				getUserInfo(scan_res);
				
			}else{
			Toast.makeText(getActivity(),"扫码失败", Toast.LENGTH_SHORT).show();
			}
		}catch(Exception e){
//			Log.e("lh", "Exception==");
			
		}
	}
	private void getUserInfo(String no){
		SharedPreferences sp = getActivity().getSharedPreferences("spReg",
				Context.MODE_PRIVATE);
		 token = sp.getString("token", "");
//		Log.e("lh", "token=="+token);
//		Log.e("lh", "no=="+no);
		if(httpTools==null){
			httpTools=new HttpTools(getActivity());
		}
		httpTools.getUserInfo(token, no, new JsonGetUserInfo());
		
	}
	
	private class JsonGetUserInfo extends JsonHttpResponseHandler{

		@Override
		public void onSuccess(int statusCode, Header[] headers,
				JSONObject response) {
			super.onSuccess(statusCode, headers, response);
//			Log.e("lh", "userinfo=="+response.toString());
			try{
				Gson gson=new Gson();
				 user=gson.fromJson(response.toString(), 
						UserInfoDataBean.class);
				if(user.isCode()){
					tv_name.setText(user.getData().getName());
					tv_phone.setText(user.getData().getPhone());
					tv_balance.setText(""+user.getData().getBalance());
					
				}else{
					Toast.makeText(getActivity(),"获取用户信息失败", 
							Toast.LENGTH_SHORT).show();
				}
			}catch(Exception e){
				Toast.makeText(getActivity(),"获取用户信息失败", 
						Toast.LENGTH_SHORT).show();
			}
			
		}
		
		@Override
		public void onFailure(int statusCode, Header[] headers, Throwable e,
				JSONObject errorResponse) {
			super.onFailure(statusCode, headers, e, errorResponse);
			Toast.makeText(getActivity(),"获取用户信息失败", 
					Toast.LENGTH_SHORT).show();
		}
	}
	
	

	

	private void recharge() {
		double money=0.0;
		String strmoney=et_money.getText().toString().trim();
		if(TextUtils.isEmpty(strmoney)){
			return;
		}
		try{
			 money=Double.parseDouble(strmoney);
		}catch(Exception e){
			Toast.makeText(getActivity(),"请输入正确的金额", 
					Toast.LENGTH_SHORT).show();
			return;
		}
		btn_recharge.setBackgroundResource(R.drawable.btn_norecharge);
		btn_recharge.setClickable(false);
		if(httpTools==null){
			httpTools=new HttpTools(getActivity());
		}
		if(TextUtils.isEmpty(token)){
			SharedPreferences sp = getActivity().getSharedPreferences("spReg",
					Context.MODE_PRIVATE);
			 token = sp.getString("token", "");
		}
//		Log.e("lh", "token=="+token+"getId=="+user.getData().getId()+
//				"money=="+money);
		httpTools.recharge(token, ""+user.getData().getId(),
				""+money,""+index,new JsonRecharge());
		
	}
	
	private class JsonRecharge extends JsonHttpResponseHandler{
		
		@Override
		public void onSuccess(int statusCode, Header[] headers,
				JSONObject response) {
			super.onSuccess(statusCode, headers, response);
//			Log.e("lh", "recharge=="+response.toString());
			btn_recharge.setBackgroundResource(R.drawable.btn_recharge);
			btn_recharge.setClickable(true);
			try{
				Gson gson=new Gson();
				DataBean data=gson.fromJson(response.toString(), 
						DataBean.class);
				if(data.isCode()){
					et_money.setText("");
					tv_balance.setText(data.getData());
					Toast.makeText(getActivity(),data.getMsg(), 
							Toast.LENGTH_SHORT).show();
					
				}else{
					Toast.makeText(getActivity(),data.getMsg(), 
							Toast.LENGTH_SHORT).show();
				}
			}catch(Exception e){
				
			}
		}
		
		@Override
		public void onFailure(int statusCode, Header[] headers,
				String responseString, Throwable throwable) {
			// TODO Auto-generated method stub
			super.onFailure(statusCode, headers, responseString, throwable);
			Log.e("lh", "JsonRecharge_onFailure=="+responseString);
			Toast.makeText(getActivity(),"充值失败", 
					Toast.LENGTH_SHORT).show();
			btn_recharge.setBackgroundResource(R.drawable.btn_recharge);
			btn_recharge.setClickable(true);
		}
	}





	
}
